package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class fileReadWrite {

	private String filePath;
	private String fileEncoding;

	public fileReadWrite() {
		filePath = "";
		fileEncoding = "";
	}
	
	public void setInfo(String fileName, String encoding) {
		filePath = fileName;
		fileEncoding = encoding;
	}
	
	public ArrayList<String> fileRead() {
		
		ArrayList<String> tempList = null;
		if(new File(filePath).exists() == false) {
			return tempList;			
		}
		
		BufferedReader in = null;
		
		try {
			in = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), fileEncoding));
			tempList = new ArrayList<String>();
			String tempString;
			while( (tempString =in.readLine()) != null) {
				tempList.add(tempString);
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			tempList.clear();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			tempList.clear();
		} catch (IOException e) {
			e.printStackTrace();
			tempList.clear();
		} catch (Exception e) {
			e.printStackTrace();
			tempList.clear();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return tempList;
	}
	
	public int fileWrite(ArrayList<String> tempList) {
		
		int ret = 0;
		BufferedWriter out = null;
		
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), fileEncoding));
			
			for(String lineString : tempList) {
				out.write(lineString + "\r\n");
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			ret = -1;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			ret = -2;
		} catch (IOException e) {
			e.printStackTrace();
			ret = -3;
		} catch (Exception e) {
			e.printStackTrace();
			ret = -4;
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	public int fileWrite(String content, boolean flag) {
		int ret = 0;
		
		BufferedWriter out = null;
		
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, flag), fileEncoding));
			
			out.write(content + "\r\n");
			
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			ret = -1;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			ret = -2;
		} catch (IOException e) {
			e.printStackTrace();
			ret = -3;
		} catch (Exception e) {
			e.printStackTrace();
			ret = -4;
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return ret;
	}
}
